package com.likui.myqq.fragment;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.likui.myqq.R;
import com.likui.myqq.adapter.NewsAdapter;
import com.likui.myqq.model.RecentChat;
import com.likui.myqq.task.AsyncTaskBase;
import com.likui.myqq.util.test.TestData;
import com.likui.myqq.view.CustomListView;
import com.likui.myqq.view.LoadingView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by helikui on 2015/9/28.
 */
public class NewsFragment extends Fragment {

    private Context mContext;
    private View mBaseView, mSearchView;
    private CustomListView mCustomListView;
    private LoadingView mLoadingView;
    private NewsAdapter adapter;

    private LinkedList<RecentChat> chats = new LinkedList<RecentChat>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mBaseView = inflater.inflate(R.layout.fragment_news, null);
        mSearchView = inflater.inflate(R.layout.common_search_l, null);
        findView();
        init();
        return mBaseView;
    }

    private void findView() {
        mCustomListView = (CustomListView) mBaseView.findViewById(R.id.lv_news);
        mLoadingView = (LoadingView) mBaseView.findViewById(R.id.loading);
    }

    private void init() {
        adapter = new NewsAdapter(mContext, chats, mCustomListView);
        mCustomListView.setAdapter(adapter);

        mCustomListView.addHeaderView(mSearchView);
        mCustomListView.setOnRefreshListener(new CustomListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new AsyncRefresh().execute(0);
            }
        });
        mCustomListView.setCanLoadMore(false);
        new NewsAsyncTask(mLoadingView).execute(0);
    }

    private class NewsAsyncTask extends AsyncTaskBase {
        List<RecentChat> recentChats = new ArrayList<>();

        public NewsAsyncTask(LoadingView loadingView) {
            super(loadingView);
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            int result = -1;
            recentChats = TestData.getRecentChats();
            if (recentChats.size() > 0) {
                result = 1;
            }
            return result;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            chats.addAll(recentChats);
            adapter.notifyDataSetChanged();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }

    private class AsyncRefresh extends AsyncTask<Integer, Integer, List<RecentChat>> {

        private List<RecentChat> recentChats = new LinkedList<RecentChat>();

        @Override
        protected List<RecentChat> doInBackground(Integer... params) {
            recentChats = TestData.getRecentChats();
            return recentChats;
        }

        @Override
        protected void onPostExecute(List<RecentChat> recentChats) {
            super.onPostExecute(recentChats);
            if (recentChats != null) {
                for (RecentChat rc : recentChats) {
                    chats.add(rc);
                }
                adapter.notifyDataSetChanged();
                mCustomListView.onRefreshComplete();
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }
}
